package com.lhy.tool.generator.impl;

import com.lhy.tool.autoconfigation.GeneratorConfiguration;
import com.lhy.tool.generator.Generator;
import com.lhy.tool.model.CrudBean;
import com.lhy.tool.util.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.IntStream;

/**
 * CustomVueUtilGenerator
 * <p/>
 * 自定义vue辅助类生成 在原有文件基础上增加
 * 1.src.router.index.js
 * 2.src.util.resource.js
 *
 * @author hyluan
 * @date 2019/1/14 18:04
 * Copyright (c) 2018 wisedu
 */
@Slf4j
public class DefaultVueUtilGenerator implements Generator {

    private final Charset UTF_8 = StandardCharsets.UTF_8;

    @Override
    @SneakyThrows
    public File genCode(CrudBean crudBean, GeneratorConfiguration generatorConfiguration) {
        writeRouterIndexJs(crudBean, generatorConfiguration);
        writeResourceJs(crudBean, generatorConfiguration);
        return null;
    }

    private void writeRouterIndexJs(CrudBean crudBean, GeneratorConfiguration generatorConfiguration) throws IOException {
        File indexJsFile = new File(generatorConfiguration.getProject().getViewProjectPath() + "src/router/index.js");
        String path = "/" + crudBean.getSimpleName().toLowerCase();
        List<String> strings = FileUtils.readLines(indexJsFile, UTF_8);
        int startIndex = 0, endIndex = 0;
        for (int i = 0; i < strings.size(); i++) {
            String string = strings.get(i);
            if ( "export default new Router({".equals(string.trim()) && "routes: [".equals(strings.get(i + 1).trim())) {
                startIndex = i + 2;
            }
            if ( "]".equals(string.trim())) {
                endIndex = i;
                break;
            }
        }
        boolean noWrite = IntStream.range(startIndex, endIndex).noneMatch(value -> strings.get(value).contains("path: '" + path + "'"));
        if (noWrite) {
            String viewPath = crudBean.getModelAttributes().getViewAttributes().getViewPath();
            String replace = viewPath.replace("src", "");
            String str = "    {path: '" + path + "', component: () => import('.." + replace + path + ".vue')},";
            strings.add(endIndex, str);
            String join = String.join("\r\n", strings);
            FileUtils.write(indexJsFile, join, UTF_8);
            log.debug("new index.js content is:{}", join);
        }
    }

    private void writeResourceJs(CrudBean crudBean, GeneratorConfiguration generatorConfiguration) throws IOException {
        File resourceJsFile = new File(generatorConfiguration.getProject().getViewProjectPath() + "src/util/resource.js");
        String resourcesContent = FileUtils.readFileToString(resourceJsFile, UTF_8);
        if (!resourcesContent.contains(String.format("export const %s", crudBean.getSimpleName()))) {
            FileUtils.write(resourceJsFile, String.format("\nexport const %s = resource('/%s', http, {})", crudBean.getSimpleName(), StringUtil.firstToLowerCase(crudBean.getSimpleName())), UTF_8, true);
        }
    }
}
